package com.yanchengtect.demo.progress.rollback.activity.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.yanchengtect.demo.progress.rollback.activity.dao.ActivityDao;
import com.yanchengtect.demo.progress.rollback.activity.dao.ApplyDao;
import com.yanchengtect.demo.progress.rollback.activity.dao.MessageDao;
import com.yanchengtect.demo.progress.rollback.activity.dao.SorderDao;
import com.yanchengtect.demo.progress.rollback.activity.dao.UserDao;
import com.yanchengtect.demo.progress.rollback.activity.domain.Activity;
import com.yanchengtect.demo.progress.rollback.activity.domain.Apply;
import com.yanchengtect.demo.progress.rollback.activity.domain.Sorder;
import com.yanchengtect.demo.progress.rollback.activity.domain.User;
import com.yanchengtect.demo.progress.rollback.activity.dto.ApplyDto;

/**
* @author 文心雕龙 Email：xuwenlong@shike8888.com
* @version 创建时间：2018年9月6日 下午6:24:22
* 类说明
*/
@Service
public class ActivityService {
	
	final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	ActivityDao activityDao;
	
	@Autowired
	SorderDao sorderDao;
	
	@Autowired
	MessageDao messageDao;
	
	@Autowired
	ApplyDao applyDao;
	
	@Autowired
	UserDao userDao;
	
	/**
	 * 回笼中奖未领取的订单  
	 * 一键回滚中奖未领取订单  
	 * @param actId 活动id
	 * @param startTime 中奖起始时间
	 * @param endTime 中奖终止时间
	 */
	@Transactional
	public void rollBackWins(int actId,Date startTime,Date endTime) {
		
		
		// 查询活动信息
		Activity act = activityDao.findById(actId);
		// 日志打印更新前数据库的数据
		log.info("act的数据库数据是:{}", JSONObject.toJSONString(act));
		
		String proName = act.getProduct_name();
		
		// 根据活动id和时间查询Sorder列表
		List<Sorder> unRecives = sorderDao.listUnReciveByAidAndTime(actId,startTime,endTime);
		// 日志打印更新前数据库的数据
		log.info("待回滚的数据库数据有{}条,内容是:{}", unRecives.size(),JSONObject.toJSONString(unRecives));

		// 删除未领取的订单
		int ret = sorderDao.DeleteUnRecives(actId,startTime,endTime);
		
		for (Sorder sorder : unRecives) {
			Integer uid = sorder.getUser_id();
			// 站内信删除记录
			messageDao.deleteByUidAndParams(uid,proName);
			// 更新申请的状态
			changeApplyStatus(uid,actId);
			// 更新用户的权重
			changeWeightByPrice(uid,act.getUnit_price(),act.getBuy_sum());
			
		}
		
		// act进行份数回滚
		act.setLeft_num(act.getLeft_num()+ret);
		
		log.info("act更新时的数据是:{}", JSONObject.toJSONString(act));
		activityDao.updateOne(act);
		
	}
	
	
	
	/**
	 * 根据活动的客单价进行权重的回滚
	 * @param uid 
	 * @param unit_price
	 * @param buy_sum
	 */
	private void changeWeightByPrice(Integer uid, Float unit_price, Integer buy_sum) {
		
		User user = userDao.findById(uid);
		// 日志打印更新前数据库的数据
		log.info("user的数据库数据是:{}", JSONObject.toJSONString(user));
		
		float price = unit_price.floatValue()*buy_sum.intValue();
		
		if (price>300) {
			user.setHigh_weight((int) Math.round(user.getHigh_weight()/0.8));
		} else {
			user.setLow_weight((int) Math.round(user.getLow_weight()/0.8));
		}
		user.setBase_weight((int) Math.round(user.getBase_weight()/0.8));
		
		log.info("user更新时的数据是:{}", JSONObject.toJSONString(user));
		userDao.updateOne(user);
	}



	/**
	 * 根据用户id和活动id回滚申请状态
	 * @param uid
	 * @param actId
	 */
	private void changeApplyStatus(int uid,int actId) {
		
		ApplyDto applyDto = applyDao.findByUidAndAid(uid,actId);
		// 日志打印更新前数据库的数据
		log.info("apply的数据库数据是:{}",JSONObject.toJSONString(applyDto));
		
		Apply apply = new Apply();
		apply.setApply_id(applyDto.getApply_id());
		// 根据申请次数回滚申请状态
		if (null != applyDto.getSecond_time()) {
			apply.setApply_status((short) 8);
		}else if(null != applyDto.getFirstTime()) {
			apply.setApply_status((short) 6);
		}else {
			apply.setApply_status((short) 2);
		}
		
		log.info("apply更新时的数据是:{}", JSONObject.toJSONString(apply));
		applyDao.updateOne(apply);
	}
}
